<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"/>
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-status-bar-style" content="black"/>
<title><?=$_GET['back']=="tao" ? '提交收货信息' : '添加收货地址';?></title>
<link rel="stylesheet" type="text/css" href="/style/css/base.css"/>
<link rel="stylesheet" type="text/css" href="/style/css/swiper.min.css"/>
<link rel="stylesheet" type="text/css" href="/style/css/xshop.css"/>
<link rel="stylesheet" type="text/css" href="/style/css/sweet-alert.css"/>
<script src="/style/js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="/style/js/swiper.min.js" type="text/javascript" charset="utf-8"></script>
<script src="/style/js/xShop.js" type="text/javascript" charset="utf-8"></script>
<script src="/style/js/picker.min.js" type="text/javascript" charset="utf-8"></script>
<script src="/style/js/sweet-alert.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="outwrap">
    <!--top-->

    <div class="meinner-adr-add">
        <div class="adr-box-add">
            <div class="iptform">
                <form action="" method="post">
                    <div class="item-ipt">
                        <span class="name">收货人</span><input type="text" id="realname" maxlength="40" placeholder="收货人姓名"
                                                            class="txt"/>
                    </div>
                    
                    
                    
                    <div class="item-ipt">
                        <span class="name">手机号码</span><input type="tel" id="mobile" maxlength="11" placeholder="11位手机号码"
                                                             class="txt"/>
                    </div>
                    <div class="item-ipt item-slt">
                        <span class="name">所在地区</span>
                        <div class="content-block">
                            <input id="sel_city" type="text" readonly="" placeholder="请选择">
                            <input id="region_value" type="hidden" value="">
                        </div>
                    </div>
                    <div class="item-ipt" style="display:none;">
                        <span class="name">身份证号码</span><input id="_idnum" type="text" placeholder="购买国内产品可不填写" class="txt"/>
                    </div>
                    <div class="item-ipt">
                        <textarea id="address" rows="" cols="" placeholder="详细地址，例如：人民路128号2栋1单元188室" maxlength="80"
                                  class="area"></textarea>
                    </div>
                </form>
            </div>
            <?php
            if($_GET['back']!="tao")
            {
            ?>
            <div class="item-white clearfix">
                <span class="set-default moren" id="_default"><i class="ico"></i>设置默认地址</span>
            </div>
            <?
            }
            ?>
            <div class="btnwrap">
                <input type="button" class="btn-mexl btn-bc" value="保存" onClick="_saveAddress();"/>
            </div>
        </div>
    </div>

</div>

<script type="text/javascript">
    var _cartid = '<?=$_GET['cartid'];?>',
        _back = '<?=$_GET['back'];?>',
        _pid = '<?=$_GET['pid'];?>';
    function testidnum(val){


        var ids = /^([0-9]{17})+([0-9x]{1})$/;

        val = val.toLowerCase(val);

        if( ids.test(val) ){
        var WI=Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2),RC=Array(1,0,"x",9,8,7,6,5,4,3,2),SUM=0,TMP=0;
        var RCSTR=val.substr(17,1),RCNUM="";
        for(var i=0;i<17;i++){
          TMP = parseInt( val.substr(i,1) );
          SUM += TMP * WI[i];
        }
        RCNUM = RC[ SUM%11 ];
        if(RCNUM==RCSTR) return true;
        }
        return false;
    }
    
    function _saveAddress() {
        var _do = true,

            par = /^(13|14|15|16|17|18|19)([0-9]{9})$/;

        if ($("#realname").prop("value") == "") {
            swal("请填写收货人姓名");
            _do = false;
        }

        if( $("#_idnum").prop("value") !="" )
          {
            if(!testidnum($("#_idnum").prop("value")))
              {
                swal("身份证号码错误");
                _do = false;
              }
          }

        if (_do && $("#mobile").prop("value") == "") {
            swal("请填写手机号码");
            _do = false;
        }
        else if (_do && $("#mobile").prop("value") != "" && !par.test($("#mobile").prop("value"))) {
            swal("手机号码格式错误");
            _do = false;
        }

        if (_do && $("#region_value").prop("value") == "") {
            swal("请选择地区");
            _do = false;
        }

        if (_do && $("#address").val() == "") {
            swal("请填写详细地址");
            _do = false;
        }

        is_default = 0;
        if ($("#_default").hasClass("moren")) is_default = 1;

        if (_do) {
            console.log(1);
            $.post("/cart/goods_ajax",
                {
                    "act": "address_add",
                    "realname": $("#realname").prop("value"),
                    "idnum": $("#_idnum").prop("value"),
                    "mobile": $("#mobile").prop("value"),
                    "region": $("#region_value").prop("value"),
                    'is_default': is_default,
                    "address": $("#address").val(),
                    "level_money":"<?=$_GET['level_money'];?>",
                    "tao":"<?=$_GET['tao'];?>",
                    "mr": Math.random()
                },
                function (json) {
                    console.log(json);
                    if (json.code == 200) {
                        if(_back=="tao")
                        {
                            swal("提交收货信息成功！", false, "success");
                        }
                        else
                        {
                            swal("添加收货地址成功", false, "success");
                        }
                        
                        setTimeout(function () {

                            if (_back == "checkout") {
                                url = "/cart/checkout?cartid=" + _cartid + "&address_id=" + json.address_id;
                            }
                            else if (_back == "addresslist") {

                                if(_pid){
                               	 url = "/cart/address?id=" + _pid;
                                    }else{

                                   	 url = "/cart/address?cartid=" + _cartid;
                                    }
                               
                            }
                            else if(_back=="tao")
                            {
                                url = "/user/index";
                            }

                            window.location.href = url;
                        }, 2100);
                    }
                    else swal("添加失败请重试", false, "error");
                }, "json");
        }

    }

    function _backPage() {
        if (_back == "checkout") {
            url = "/cart/checkout?cartid=" + _cartid;
        }
        else if (_back == "addresslist") {
            url = "/cart/address?cartid=" + _cartid;
        }
        window.location.href = url;
    }

    /*区域选择*/

    var city = <?=$this->region;?>;

    var nameEl = document.getElementById('sel_city');

    var first = [];
    /* 省，直辖市 */
    var second = [];
    /* 市 */
    var third = [];
    /* 镇 */

    var selectedIndex = [0, 0, 0];
    /* 默认选中的地区 */

    var checked = [0, 0, 0];
    /* 已选选项 */

    function creatList(obj, list) {
        obj.forEach(function (item, index, arr) {
            var temp = new Object();
            temp.text = item.name;
            temp.value = item.id;
            list.push(temp);
        })
    }

    creatList(city, first);

    if (city[selectedIndex[0]].hasOwnProperty('sub')) {
        creatList(city[selectedIndex[0]].sub, second);
    } else {
        second = [{text: '', value: 0}];
    }

    if (city[selectedIndex[0]].sub[selectedIndex[1]].hasOwnProperty('sub')) {
        creatList(city[selectedIndex[0]].sub[selectedIndex[1]].sub, third);
    } else {
        third = [{text: '', value: 0}];
    }

    var picker = new Picker({
        data: [first, second, third],
        selectedIndex: selectedIndex
    });

    picker.on('picker.select', function (selectedVal, selectedIndex) {
        var text1 = first[selectedIndex[0]].text;
        var text2 = second[selectedIndex[1]].text;
        var text3 = third[selectedIndex[2]] ? third[selectedIndex[2]].text : '';

        var value1 = first[selectedIndex[0]].value;
        var value2 = second[selectedIndex[1]].value;
        var value3 = third[selectedIndex[2]] ? third[selectedIndex[2]].value : 0;

        $("#sel_city").prop("value", text1 + ' ' + text2 + ' ' + text3);
        $("#region_value").prop("value", value1 + ',' + value2 + ',' + value3);
    });

    picker.on('picker.change', function (index, selectedIndex) {
        if (index === 0) {
            firstChange();
        } else if (index === 1) {
            secondChange();
        }

        function firstChange() {
            second = [];
            third = [];
            checked[0] = selectedIndex;
            var firstCity = city[selectedIndex];
            if (firstCity.hasOwnProperty('sub')) {
                creatList(firstCity.sub, second);

                var secondCity = city[selectedIndex].sub[0]
                if (secondCity.hasOwnProperty('sub')) {
                    creatList(secondCity.sub, third);
                } else {
                    third = [{text: '', value: 0}];
                    checked[2] = 0;
                }
            } else {
                second = [{text: '', value: 0}];
                third = [{text: '', value: 0}];
                checked[1] = 0;
                checked[2] = 0;
            }

            picker.refillColumn(1, second);
            picker.refillColumn(2, third);
            picker.scrollColumn(1, 0)
            picker.scrollColumn(2, 0)
        }

        function secondChange() {
            third = [];
            checked[1] = selectedIndex;
            var first_index = checked[0];
            if (city[first_index].sub[selectedIndex].hasOwnProperty('sub')) {
                var secondCity = city[first_index].sub[selectedIndex];
                creatList(secondCity.sub, third);
                picker.refillColumn(2, third);
                picker.scrollColumn(2, 0)
            } else {
                third = [{text: '', value: 0}];
                checked[2] = 0;
                picker.refillColumn(2, third);
                picker.scrollColumn(2, 0)
            }
        }

    });

    picker.on('picker.valuechange', function (selectedVal, selectedIndex) {

    });

    nameEl.addEventListener('click', function () {
        picker.show();
    });
</script>
</body>
</html>
